# -*- coding: utf-8 -*-
# coding:unicode_escape


import time
from parking_lot.car import Car


class ParkingLot(object):
    """
    停车场类，属性有停车场名、容量、已停车辆数据
    """
    
    
    def __init__(self, parking_name, capacity):
        super(ParkingLot, self).__init__()
        self.parking_name = parking_name
        self.capacity = capacity
        self.car_list = []

    def get_parking_name(self):
        return self.parking_name

    def get_capacity(self):
        return self.capacity
        
    def get_cars(self):
        return self.car_list


class Parking(object):
    """
    停车类，可根据不同策略停车
    """
    
    
    def __init__(self, parking_info_list, plate_number, parking_strategy):
        """
        param1: 包含管理的所有停车场数据信息，是个列表，每个元素是个元组，("停车场名", "停车容量", "已停入的车辆数据")
        param2: 要停入的车辆车牌号
        param3: 停车场停车策略，取值：seq(顺序停车)/max_first(选择剩余车位最多的停入)/min_first(选择剩余车位最少的停入)
        """
        
        super(Parking, self).__init__()
        self.parking_info_list = parking_info_list
        self.parking_strategy = parking_strategy
        self.parking_number = len(self.parking_info_list)
        
        if self.parking_strategy == "seq":
            self.park_sequence(plate_number)
        if self.parking_strategy == "max_first":
            self.park_free_first(plate_number, "max")
        if self.parking_strategy == "min_first":
            self.park_free_first(plate_number, "min")

    def park_sequence(self, plate_number):
        """
        按顺序停车策略，停满第一个停车场再停第二个停车场
        param1: 要停入的车辆车牌号
        """
        
        total_capacity = self.get_total_capacity()
        cur_cars = self.get_cur_cars()
        
        if cur_cars == total_capacity:
            print("对不起，没有空车位，请去其它停车场。")
            return False
        
        for index in range(0, self.parking_number):
            free_parking = self.get_free_parking(self.parking_info_list[index])
            if free_parking > 0:
                self.park(self.parking_info_list[index], plate_number)
                break
            
    def park_free_first(self, plate_number, free_first_strategy):
        """
        优先停车策略
        param1: 要停入车辆的车牌号
        param2: 停车策略的优先原则，取值：min(已有车辆最少则优先)/max(已有车辆最多则优先)
        """
        
        total_capacity = self.get_total_capacity()
        cur_cars = self.get_cur_cars()
        
        if cur_cars == total_capacity:
            print("对不起，没有空车位，请去其它停车场。")
            return False

        index, parking_name = self.get_max_min_free_parking(free_first_strategy)
        print(index, parking_name)
        if index is not None:
            self.park(self.parking_info_list[index], plate_number)
            

    def park(self, parking_info, plate_number):
        """
        停车
        param1: 即将停入的停车场信息，为parking_info_list的一个元素
        param2: 要停入车辆的车牌号
        """
        
        parking_name = parking_info[0]
        plate_number = plate_number
        starttime = time.time()
        car = Car(plate_number, starttime)
        parking_info[2].append(car)
        print("车牌号为 %s 的车辆已停入停车场 %s ，停入时间 %s 。" % (plate_number, parking_name, time.ctime(starttime)))

    def get_total_capacity(self):
        """
        获取管理的所有停车场容量和
        """
        
        total_capacity = 0
        
        try:
            for index in range(0, self.parking_number):
                total_capacity += self.parking_info_list[index][1]
                
            return total_capacity
        except Exception as e:
            print(e)
            
    def get_cur_cars(self):
        """
        获取当前管理的所有停车场已停入的车辆总数
        """
        
        cur_cars = 0
        
        try:
            for index in range(0, self.parking_number):
                cur_cars += len(self.parking_info_list[index][2])
                
            return cur_cars
        except Exception as e:
            print(e)

    def get_free_parking(self, parking_info):
        """
        获取停车场剩余车位数
        """
        
        cur_cars = len(parking_info[2])
        capacity = parking_info[1]
        free_parking = capacity - cur_cars
        return free_parking
        
    def get_max_min_free_parking(self, extremum):
        """
        获取所管理的停车场里剩余车位数最多/少的停车场信息
        param1: 取值min/max，分别代表最多和最少
        return: 返回满足条件的停车场顺序号、停车场名
        """
        
        free_parking_list = []
        free_counts_list = []
        
        try:
            for index in range(0, self.parking_number):
                free_parking = self.get_free_parking(self.parking_info_list[index])
                parking_name = self.parking_info_list[index][0]
                free_parking_list.append((parking_name, free_parking))
                free_counts_list.append(free_parking)
            
            for i in range(len(free_counts_list)-1,-1,-1):
                if free_counts_list[i] == 0:
                    del free_counts_list[i]
            
            if len(free_counts_list) == 0:
                return None, None
            
            if extremum == "max":
                extremum_free_parking = max(free_counts_list)
            elif extremum == "min":
                extremum_free_parking = min(free_counts_list)
            
            for index in range(0, self.parking_number):
                if extremum_free_parking == free_parking_list[index][1]:
                    extremum_parking_name = free_parking_list[index][0]
                    extremum_parking_index = index
                    return extremum_parking_index, extremum_parking_name
            
        except Exception as e:
            print(e)
